# -*-coding:utf-8 -*-

"""
# File       : trafficRequest.py
# Time       ：2023/3/31 下午11:52
# Author     ：puppet
# version    ：python 3.8.1
# Description：
"""
from mitmproxyModel.models import autoUploadedAPI
import requests
import threading
import json
from mitmproxyModel.TrafficReplay.jsonDifferent import showJsonDiff

# articleObj = Article.objects.filter(Q(isdeleted=0) & Q(onshelf=1)).values('id', 'title', 'subtitle', 'introduction',
#                                                                           'content', 'link', 'keyword', 'classfy',
#                                                                           'hits', 'viewnum', 'ontop',
#                                                                           'iselite', 'author', 'create_time',
#                                                                           'update_time').order_by('-update_time')[
#              :articleCount]
# print(list(articleObj))


class ReplayApi():
    def __init__(self,id):
        self.id = id

    def getApidata(self):
        print("id", self.id)
        replyObj = autoUploadedAPI.objects.filter(id=self.id).values("reqMethod","reqUrl", "reqContent","ReqReferer","json")
        print("replyObj", replyObj)
        url = dict(replyObj[0]).get("reqUrl")
        reqContent = dict(replyObj[0]).get("reqContent")
        method = dict(replyObj[0]).get("reqMethod")
        ReqReferer = dict(replyObj[0]).get("ReqReferer")
        Resjson = dict(replyObj[0]).get("json")
        header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36",
            # "Content-Type":"application/json",
            "Referer": f"{ReqReferer}",
        }
        tempresultData = {}
        if method == "POST":
            res = requests.post(url=url, json=reqContent,headers=header, timeout=5)
            if res.status_code == 200:
                tempresultData['response'] = res.json()
            else:
                tempresultData['response'] = {"msg":f"Response of replay traffic is wrong, status_code is {res.status_code}"}
        elif method == "GET":
            res = requests.get(url=url,timeout=5)
            if res.status_code == 200:
                tempresultData['response'] = res.json()
            else:
                tempresultData['response'] = {"msg":f"Response of replay traffic is wrong, status_code is {res.status_code}"}
        originalJson = {} if Resjson == '' else eval(Resjson)
        tempresultData.get('response')
        jsonFromRequest ={} if tempresultData.get('response') == None else  dict(tempresultData.get('response'))
        diffResult = showJsonDiff(originalJson, jsonFromRequest)
        return diffResult

    # def sendRequest(self):
    #     threadObj = threading.Thread(target=self.getApidata())
    #     threadObj.start()

if __name__ == '__main__':
    # replyObj = ReplayApi(id=285).sendRequest()
    url = "https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all"
    res = requests.get(url=url)
